<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\db\Query;

class TfOrder extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tf_order}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function selectData($data = [] ,$field = '*')
    {
        $condition = '`comment`.deleted_at is null';
        $condition .= ' and `comment`.status = '.self::STATUS;
        if(!empty($data['data'])){
            $condition .= " and j.title like '%".$data['data']."%'";
        }
        if(!empty($data['member'])){
            $condition .= " and ( (m.username like '%".$data['member']."%') or (member_id = '".$data['member']."'))";
        }
        if(!empty($data['status'])){
            $condition .= ' and `comment`.status = '.$data['status'];
        }
        $list = (new Query())
            ->select($field)
            ->from('comment')
            ->leftJoin('posts j','`comment`.`posts_id` = `j`.`id`')
            ->leftJoin('member m','`comment`.`member_id` = `m`.`id`')
            ->orderBy('comment.created_at desc')
            ->where($condition)
            ->all();
        return empty($list)?array():$list;
    }

    /**
     * @inheritdoc
     */
    public static function findInfo($data = [] ,$field = '*')
    {
        $condition = 'delete_time is null';
        $condition .= ' and status > '.self::STATUS;
        if(!empty($data['uid'])){
            $condition .= ' and uid = '.$data['uid'];
        }
        if(!empty($data['project_id'])){
            $condition .= ' and project_id = '.$data['project_id'];
        }
        $list = (new Query())
            ->select($field)
            ->from('tf_order')
            ->where($condition)
            ->one();
        return empty($list)?array():$list;
    }


}
